package com.ecy.quadratic.ui.fragment;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ImagePagerAdapter;
import com.ecy.quadratic.adapter.NovelForthRecommendAdapter;
import com.ecy.quadratic.adapter.NovelOneRecommendAdapter;
import com.ecy.quadratic.adapter.NovelSecondRecommendAdapter;
import com.ecy.quadratic.component.DaggerNovelMainComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.bean.BooksByCats;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.reader.ui.activity.BookDetailActivity;
import com.ecy.quadratic.ui.activity.ComicRankActivity;
import com.ecy.quadratic.ui.contract.NovelRecommendContract;
import com.ecy.quadratic.ui.presenter.NovelRecommendPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.yuyh.easyadapter.glide.GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;


/**
 * Created by Administrator on 2016/3/31.
 */
public class NovelRecommendFragment extends BaseFragment implements NovelRecommendContract.View {

    @Bind(R.id.recommend_swiperefresh_layout)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.activity_grid_view__main)
    LinearLayout mContentLlt;
    @Bind(R.id.recommend_gridview)
    GridView mGridView;
    @Bind(R.id.recommend_update_gridview)
    GridView mGridUpdateView;
    @Bind(R.id.recommend_joke_gridview)
    GridView mGridJokeView;
    @Bind(R.id.recommend_ancient_gridview)
    GridView mGridAncientView;
    @Bind(R.id.recommend_love_gridview)
    GridView mGridLoveView;
    @Bind(R.id.recommend_fantasy_gridview)
    GridView mGridFantasyView;
    @Bind(R.id.recommend_thriller_gridview)
    GridView mGridThrillerView;
    @Bind(R.id.recommend_tanbi_gridview)
    GridView mGridTanbiView;
    @Bind(R.id.recommend_guess_gridview)
    GridView mGridGuessView;
    @Bind(R.id.recommend_top_viewpager)
    AutoScrollViewPager mViewPager;
    @Bind(R.id.dot_0)
    View dot_0_view;
    @Bind(R.id.dot_1)
    View dot_1_view;
    @Bind(R.id.dot_2)
    View dot_2_View;
    @Bind(R.id.dot_3)
    View dot_3_View;
    @Bind(R.id.dot_4)
    View dot_4_View;
    @Bind(R.id.rank_finished_iv)
    ImageView mRankFinishedIV;
    @Bind(R.id.rank_update_iv)
    ImageView mRankUpdateIV;
    @Bind(R.id.rank_rank_iv)
    ImageView mRankRankIV;

    @Inject
    NovelRecommendPresenter mRecommendPresenter;

    private NovelSecondRecommendAdapter mRecommendAdapter;
    private NovelOneRecommendAdapter mRecommendUpdateAdapter;
    private NovelForthRecommendAdapter mRecommendJokeAdapter;
    private NovelOneRecommendAdapter mRecommendFantasyAdapter;
    private NovelOneRecommendAdapter mRecommendAncientAdapter;
    private NovelForthRecommendAdapter mRecommendThrillerAdapter;
    private NovelSecondRecommendAdapter mRecommendLoveAdapter;
    private NovelOneRecommendAdapter mRecommendTanbiAdapter;
    private NovelOneRecommendAdapter mRecommendGuessAdapter;
    private List<BooksByCats.BooksBean> mComicList;
    private List<BooksByCats.BooksBean> mComicUpdateList;
    private List<BooksByCats.BooksBean> mComicJokeList;
    private List<BooksByCats.BooksBean> mComicAncientList;
    private List<BooksByCats.BooksBean> mComicFantasyList;
    private List<BooksByCats.BooksBean> mComicLoveList;
    private List<BooksByCats.BooksBean> mComicThrillerList;
    private List<BooksByCats.BooksBean> mComicTanbiList;
    private List<BooksByCats.BooksBean> mComicGuessList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;


    private List<ImageView> images;
    private List<View> dots;
    //记录上一次点的位置
    private int oldPosition = 0;
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mRecommendPresenter.getRecommendList("1");
        }
    };

    public NovelRecommendFragment() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_novel_recommend;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerNovelMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mRecommendPresenter != null) {
            mRecommendPresenter.attachView(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRecommendPresenter != null)
            mRecommendPresenter.detachView();
    }

    @Override
    public void initData() {
        mComicList = new ArrayList<>();
        mComicUpdateList = new ArrayList<>();
        mComicJokeList = new ArrayList<>();
        mComicFantasyList = new ArrayList<>();
        mComicAncientList = new ArrayList<>();
        mComicThrillerList = new ArrayList<>();
        mComicLoveList = new ArrayList<>();
        mComicTanbiList = new ArrayList<>();
        mComicGuessList = new ArrayList<>();
        mRecommendAdapter = new NovelSecondRecommendAdapter(getContext(), mComicList);
        mRecommendUpdateAdapter = new NovelOneRecommendAdapter(getContext(), mComicUpdateList);
        mRecommendLoveAdapter = new NovelSecondRecommendAdapter(getContext(), mComicLoveList);
        mRecommendFantasyAdapter = new NovelOneRecommendAdapter(getContext(), mComicFantasyList);
        mRecommendTanbiAdapter = new NovelOneRecommendAdapter(getContext(), mComicTanbiList);
        mRecommendJokeAdapter = new NovelForthRecommendAdapter(getContext(), mComicJokeList);
        mRecommendAncientAdapter = new NovelOneRecommendAdapter(getContext(), mComicAncientList);
        mRecommendThrillerAdapter = new NovelForthRecommendAdapter(getContext(), mComicThrillerList);
        mRecommendGuessAdapter = new NovelOneRecommendAdapter(getContext(), mComicGuessList);
    }

    @Override
    public void configViews() {
        Glide.with(getActivity()).load(R.drawable.rank_novel_finished).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankFinishedIV);
        Glide.with(getActivity()).load(R.drawable.rank_novel_update).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankUpdateIV);
        Glide.with(getActivity()).load(R.drawable.rank_novel_rank).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankRankIV);
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                mSwipeRefreshView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendAdapter.getItem(position).getId());
            }
        });
        mGridUpdateView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendUpdateAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendUpdateAdapter.getItem(position).getId());
            }
        });
        mGridJokeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendJokeAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendJokeAdapter.getItem(position).getId());
            }
        });
        mGridLoveView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendLoveAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendLoveAdapter.getItem(position).getId());
            }
        });
        mGridFantasyView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendFantasyAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendFantasyAdapter.getItem(position).getId());
            }
        });
        mGridAncientView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendAncientAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendAncientAdapter.getItem(position).getId());
            }
        });
        mGridThrillerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendThrillerAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendThrillerAdapter.getItem(position).getId());
            }
        });
        mGridTanbiView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendTanbiAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendTanbiAdapter.getItem(position).getId());
            }
        });
        mGridGuessView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookDetailActivity.startActivity(getContext(), mRecommendGuessAdapter.getItem(position)._id);
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendTanbiAdapter.getItem(position).getId());
            }
        });
        mGridView.setAdapter(mRecommendAdapter);
        mGridUpdateView.setAdapter(mRecommendUpdateAdapter);
        mGridJokeView.setAdapter(mRecommendJokeAdapter);
        mGridLoveView.setAdapter(mRecommendLoveAdapter);
        mGridFantasyView.setAdapter(mRecommendFantasyAdapter);
        mGridAncientView.setAdapter(mRecommendAncientAdapter);
        mGridThrillerView.setAdapter(mRecommendThrillerAdapter);
        mGridTanbiView.setAdapter(mRecommendTanbiAdapter);
        mGridGuessView.setAdapter(mRecommendGuessAdapter);
        final ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(R.drawable.c);
        list.add(R.drawable.b);
        list.add(R.drawable.c);
        list.add(R.drawable.d);
        list.add(R.drawable.b);

        mViewPager.setAdapter(new ImagePagerAdapter(getContext(), list).setInfiniteLoop(true));
        mViewPager.setScrollDurationFactor(3);
        mViewPager.setInterval(3000);
        mViewPager.startAutoScroll();
        mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % list.size());
        //显示的小点
        dots = new ArrayList<View>();
        dots.add(dot_0_view);
        dots.add(dot_1_view);
        dots.add(dot_2_View);
        dots.add(dot_3_View);
        dots.add(dot_4_View);

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        mSwipeRefreshView.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mSwipeRefreshView.setEnabled(true);
                }
                return false;
            }
        });

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                position = position % list.size();
                dots.get(position).setBackgroundResource(R.drawable.dot_focused);
                dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);

                oldPosition = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
        mViewPager.startAutoScroll();
    }

    @Override
    public void onStop() {
        super.onStop();
        mViewPager.stopAutoScroll();
    }

    @OnClick(R.id.rank_finished_iv)
    public void onFinishedRank() {
        ComicRankActivity.startActivity(getContext());
    }

    @OnClick(R.id.rank_update_iv)
    public void oUpdateRank() {
        ComicRankActivity.startActivity(getContext());
    }

    @OnClick(R.id.rank_rank_iv)
    public void onRank() {
        ComicRankActivity.startActivity(getContext());
    }

    @Override
    public void showRecommendList(BooksByCats cats) {
        List<BooksByCats.BooksBean> list = cats.books;
        mContentLlt.setVisibility(View.VISIBLE);
//		mCurrentPage = pageInfo.pageNum;
//		mTotalPage = pageInfo.pages;
        mComicList.clear();
        mComicUpdateList.clear();
        mComicJokeList.clear();
        mComicThrillerList.clear();
        mComicLoveList.clear();
        mComicFantasyList.clear();
        mComicAncientList.clear();
        mComicTanbiList.clear();
        mComicGuessList.clear();
        mComicList.addAll(list.subList(0, 4));
        mComicUpdateList.addAll(list.subList(3, 7));
        mComicJokeList.addAll(list.subList(6, 10));
        mComicThrillerList.addAll(list.subList(9, 13));
        mComicLoveList.addAll(list.subList(12, 16));
        mComicFantasyList.addAll(list.subList(15, 19));
        mComicAncientList.addAll(list.subList(13, 17));
        mComicTanbiList.addAll(list.subList(17, 21));
        mComicGuessList.addAll(list);
        mRecommendAdapter.notifyDataSetChanged();
        mRecommendUpdateAdapter.notifyDataSetChanged();
        mRecommendLoveAdapter.notifyDataSetChanged();
        mRecommendTanbiAdapter.notifyDataSetChanged();
        mRecommendFantasyAdapter.notifyDataSetChanged();
        mRecommendJokeAdapter.notifyDataSetChanged();
        mRecommendAncientAdapter.notifyDataSetChanged();
        mRecommendThrillerAdapter.notifyDataSetChanged();
        mRecommendGuessAdapter.notifyDataSetChanged();

//		Glide.with(getActivity()).load(mComicFantasyList.get(mComicFantasyList.size()-1).getImg()).centerCrop().into(mFantasyImageView);
    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
    }
}
